package main

import (
	"net/http"
	"io/ioutil"
	"encoding/json"

	"github.com/Sugobet/SGBbot"
	"github.com/Sugobet/SGBbot/event"
	"github.com/Sugobet/SGBbot/api"
)

// SGBbot调用智能对话api实现智能聊天机器人简单示例

var isZhiNengDuiHua bool


func isZNDH(event *event.GroupMessage){
	if event.UserID == 321355478{
		if event.Message == "开启智能对话"{
			isZhiNengDuiHua = true
			api.SendGroupMsg(event.GroupID, "已开启智能对话", false)
			return
		}
		if event.Message == "关闭智能对话"{
			isZhiNengDuiHua = false
			api.SendGroupMsg(event.GroupID, "已关闭智能对话", false)
			return
		}
	}
}


var apiurl = "http://api.qingyunke.com/api.php?key=free&msg="
func zNDH(event *event.GroupMessage){
	if isZhiNengDuiHua == true{
		res, err := http.Get(apiurl+event.Message)
		if err != nil{
			api.SendGroupMsg(event.GroupID, "gg", false)
			return
		}
		defer res.Body.Close()

		bod, err := ioutil.ReadAll(res.Body)
		if err != nil{
			return
		}
		var ma map[string]interface{}
		json.Unmarshal(bod, &ma)
		api.SendGroupMsg(event.GroupID, ma["content"].(string), false)
	}
}


func main(){
	api.AccessToken = "you access_token"	// 若在cqhttp api插件配置文件里设置了access_token, 则这里需要填上，没有设置则留空
	api.Addr = "127.0.0.1:5700"		// coolqhttp api 配置文件里的host:port

	bot.OnGroupMessage(isZNDH)	// 将函数isZNDH添加到监听群组消息任务里
	bot.OnGroupMessage(zNDH)
	bot.Run("127.0.0.1:9999")	// 开启WebSocket获取事件  ws_reverse_event_url的host:port
}
